package dao.Impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import dao.BookInfoDao;
import entity.BookInfo;
import util.C3P0Util;

public class BookInfoDaoImpl implements BookInfoDao {

	@Override
	public List<BookInfo> getAllBookInfo(Integer pageNo,Integer pageSize,HashMap<String, String> map) {
		List<BookInfo> list = null;
		
		//操作数据可对象
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		
		//编写sql语句   where 1=1无论是什么sql语句都可以拼接查询  
		String sql = "select * from bookInfo where 1=1";
		//调用StringBuffer进行Sql语句拼接
		StringBuffer sb = new StringBuffer();
		sb.append(sql);
		//根据map中的数据进行sql拼接
		//判断key的条件
		if (map.get("bookName") != null && !map.get("bookName").trim().equals("")) {
			sb.append(" and bookName like '%"+map.get("bookName").trim()+"%'");
		}
		if (map.get("typeID") != null && !map.get("typeID").trim().equals("")) {
			sb.append(" and typeID ="+map.get("typeID").trim());
		}
		//拼接分页sql语句
		sb.append(" limit ?,?");
		System.out.println(sb);
		
		try {
			list =  qr.query(sb.toString(),
					new Integer[]{(pageNo-1)*pageSize,pageSize} ,
					new BeanListHandler<BookInfo>(BookInfo.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Integer getRows(HashMap<String, String> map) {
		
		//操作数据可对象
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
				
		//编写sql语句
		String sql = "select count(bookId) number from bookinfo where 1=1 ";
		//调用StringBuffer进行Sql语句拼接
			StringBuffer sb = new StringBuffer();
			sb.append(sql);
			//根据map中的数据进行sql拼接
			//判断key的条件
			if (map.get("bookName") != null && !map.get("bookName").trim().equals("")) {
				sb.append(" and bookName like '%"+map.get("bookName").trim()+"%'");
			}
			if (map.get("typeID") != null && !map.get("typeID").trim().equals("")) {
					sb.append(" and typeID ="+map.get("typeID").trim());
			}
			System.out.println(sb);
		//查询结果
		Long i = null;
		
		 try {
			i =(Long)qr.query(sql, new ScalarHandler("number"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i.intValue();
	}

	@Override
	public boolean insertBookInfo(BookInfo bookInfo) {
		//操作数据可对象
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		
		//编写sql语句   where 1=1无论是什么sql语句都可以拼接查询  
		String sql = "INSERT INTO `bookinfo` VALUE( NULL,?,?,?,?);";
		boolean flag = false;
		try {
			int i = qr.update(sql,new Object[]{bookInfo.getBookName(),bookInfo.getTypeID(),
					bookInfo.getCreateDate(),bookInfo.getImg()});
			if (i==1) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}


}
